#include <fstream>

#ifdef _DEBUG
#include "../Misc/DebugOutput.h"
#endif

template <typename MeshType>
map<string, MeshType*> GeometryLoader<MeshType>::animatedMeshMap;

template <typename MeshType>
GeometryLoader<MeshType>::~GeometryLoader()
{
	map<string, MeshType*>::iterator ite = animatedMeshMap.begin();

	while(ite != animatedMeshMap.end())
	{
		delete ite->second;
		ite->second = 0;
		textureMap.erase(ite);
		ite = textureMap.begin();
	}
}

template <typename MeshType>
bool GeometryLoader<MeshType>::LoadStaticGeometry(const char* _filePath, MeshType& _mesh)
{
	std::fstream in;
	in.open(_filePath, std::ios_base::in | std::ios_base::binary);

	unsigned int numVertices = 0;
	unsigned int numTriangles = 0;
	unsigned int numTextures = 0;
	unsigned int nameLength = 0;

	if(in.good())
	{
		in.read((char *)&numVertices, sizeof(numVertices));

		_mesh.vertices.resize(numVertices);
		in.read((char *)&_mesh.vertices[0], (numVertices)*sizeof(_mesh.vertices[0]));

		in.read((char *)&numTriangles, sizeof(numTriangles));
		_mesh.indices.resize(numTriangles*3);

		in.read((char *)&_mesh.indices[0], numTriangles*3*sizeof(_mesh.indices[0]));
	}
	else
	{
		in.close();

		#ifdef _DEBUG
			stringstream error;
			error	<< "MESH FROM FILE: " << _filePath;
			DebugOutput::Print(DebugOutput::RENDER_MSG, DebugOutput::FAILURE_CREATE, error.str().c_str());
		#endif
		
		return false;
	}
	
	in.close();

	#ifdef _DEBUG
		stringstream success;
		success	<< "MESH FROM FILE: " << _filePath;
		DebugOutput::Print(DebugOutput::RENDER_MSG, DebugOutput::SUCCESS_CREATE, success.str().c_str());
	#endif

	return true;
}